package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SystemNotification;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SystemNotificationMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;
    @Autowired
    private SystemNotificationMapper systemNotificationMapper;

    SystemNotification systemNotification = new SystemNotification();

    @Override
    public List<Travel> query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list=travelMapper.selectForList(qo);
        return list;
    }


    @Override
    public void saveOrUpdate(Travel travel) {
        if (travel.getId() != null) {
            travelMapper.updateByPrimaryKey(travel);
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            contentMapper.updateByPrimaryKey(travelContent);
        }else {
            travel.setAuthor(UserContext.getCurrentUser());
            travelMapper.insert(travel);
            contentMapper.insert(travel.getTravelContent().getContent(),travel.getId());

            //系统通知
            systemNotification.setTravel(travel);
            systemNotification.setState(4);
            systemNotification.setUser(travel.getAuthor());
            systemNotification.setType(1L);
            systemNotificationMapper.insert(systemNotification);

        }
    }

    @Override
    public Travel selectByTravelId(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateState(Travel travel) {
        travel.setReleaseTime(new Date());
        travelMapper.updateState(travel);

        //系统通知
        if (travel.getState()== 2) {
            //系统通知
            systemNotification.setTravel(travel);
            systemNotification.setState(2);
            systemNotification.setUser(travel.getAuthor());
            systemNotification.setType(1L);
            systemNotificationMapper.insert(systemNotification);
            System.out.println(travel.getAuthor());
        }else if (travel.getState()== -1){
            //系统通知
            systemNotification.setTravel(travel);
            systemNotification.setState(0);
            systemNotification.setUser(travel.getAuthor());
            systemNotification.setType(1L);
            systemNotificationMapper.insert(systemNotification);
        }
    }

    @Override
    public TravelContent selectContentByTravelId(Long id) {

        return contentMapper.selectByPrimaryKey(id);
    }

    public Integer getCollectionById(Long travelId, Long userId,int type) {
        return travelMapper.getCollectionById(travelId,userId,type);
    }

    public void save(Long travelId, Long userId, int type) {
        travelMapper.save(travelId,userId,type);
    }

    public void delete(Long id, Long userId, int type) {
        travelMapper.dalete(id,userId,type);
    }

    /** qyq
     * 查询用户的游记数量
     * @param userId
     * @return
     */
    public int getTravelNum(Long userId) {
        return travelMapper.getTravelNum(userId);
    }

}
